package team.seekers.hula.course.courseUser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.course.courseUser.entity.dto.CourseUserBatchBindDTO;
import team.seekers.hula.course.courseUser.entity.dto.CourseUserBindDTO;
import team.seekers.hula.course.courseUser.entity.dto.CourseUserPageQueryDTO;
import team.seekers.hula.course.courseUser.entity.dto.CourseUserUpdateDTO;
import team.seekers.hula.course.courseUser.entity.vo.CourseUserBindInfoListVO;
import team.seekers.hula.course.courseUser.entity.vo.CourseUserManageInfoVO;
import team.seekers.hula.course.courseUser.entity.vo.CourseUserManageListVO;
import team.seekers.hula.course.courseUser.service.CourseUserManageService;
import team.seekers.hula.course.courseUser.service.CourseUserService;
import team.seekers.hula.auth.service.MindUtils;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/manage/course/user")
@ApiResponseBody
public class CourseUserManageController {

    @Autowired
    private CourseUserService courseUserService;

    @Autowired
    private CourseUserManageService courseUserManageService;


    /**
     * 为当前选中的所有人绑定所有课程
     */
    @PostMapping("/batch-bind")
    public void batchBind(@RequestBody CourseUserBatchBindDTO courseUserBatchBindDTO){
        courseUserManageService.batchBind(courseUserBatchBindDTO);
    }

    /**
     * 用户课程绑定
     */
    @PostMapping("/bind")
    public void bind(@RequestBody CourseUserBindDTO courseUserBindDTO){
        ArrayList<String> userIds = new ArrayList<>();
        userIds.add(courseUserBindDTO.getUserId());
        ArrayList<String> courseIds = new ArrayList<>();
        courseIds.add(courseUserBindDTO.getCourseId());
        CourseUserBatchBindDTO courseUserBatchBindDTO = CourseUserBatchBindDTO.builder().courseIds(courseIds).userIds(userIds).role(courseUserBindDTO.getRole()).build();
        courseUserManageService.batchBind(courseUserBatchBindDTO);
    }

    /**
     * 用户课程解绑
     * @param userId 用户id
     * @param courseId 课程id
     */
    @GetMapping("/unbind")
    public void unbind(@RequestParam String userId,
                       @RequestParam String courseId) {
        courseUserManageService.unbind(userId,courseId);
    }

    /**
     * 同步所有的进度统计，包括期末测试的成绩（同时也是定时任务，每个小时执行一次）
     */
    @Scheduled(cron = "0 * */1 * * ?")
    @GetMapping("/progress/sync")
    public void sync() {
        courseUserService.syncProgress();
    }

    /**
     * 详细的学习记录
     */
    @GetMapping("/progress/info")
    public CourseUserManageInfoVO info(@RequestParam String courseId) {
        return courseUserService.getProgressInfoByCourseUserId(courseId, MindUtils.getUserId());
    }

    @GetMapping("/list")
    public List<CourseUserBindInfoListVO> list(@RequestParam String userId) {
        return courseUserService.listUserBindCourseList(userId);
    }

    @GetMapping("/updaterole")
    public void updateRole(CourseUserUpdateDTO userUpdateDTO) {
        courseUserService.updateRole(userUpdateDTO);
    }

    @GetMapping("/page")
    public PageResult<CourseUserManageListVO> list(CourseUserPageQueryDTO userQuery) {
        if(userQuery.getNotIn() != null && userQuery.getNotIn() == 1){
            return courseUserService.pageNotIn(userQuery);
        }else{
            return courseUserService.page(userQuery);
        }
    }


}
